<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="分类管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="400px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="商品类型">
                <el-select v-model="formData.goodsTypeId" filterable placeholder="请选择" :disabled="goodsTypeDisabled" style="width:200px;">
                    <el-option
                        v-for="item in goodsTypeAllList"
                        :key="item.id"
                        :label="item.goodsTypeName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="分类名称" prop="goodsClassName">
                <el-input v-model="formData.goodsClassName"/>
            </el-form-item>

            <el-form-item label="图片" prop="image">
                <el-upload
                    class="avatar-uploader"
                    :action="apiHost + '/file/upload'"
                    :show-file-list="false"
                    :on-success="handleSuccess"
                    :headers="headers"
                >
                    <div v-if="formData.image">
                        <img :src="formData.image" class="avatar">
                    </div>
                    <i v-else class="el-icon-plus avatar-uploader-icon"/>
                </el-upload>
                <div class="form-tips">尺寸：300*300</div>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存分类吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {GoodsClassApi} from '@/api/mall/goodsClass'
import {GoodsTypeApi} from '@/api/mall/goodsType'
import {Common} from '@/utils/common'
import { getToken } from '@/utils/auth'

export default {
    name: 'GoodsClassUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                goodsTypeId: 0,
                parentId: 0,
                goodsClassName: '',
                image: '',
            },
            rules: {
                goodsClassName: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            goodsTypeAllList: [],
            apiHost: process.env.VUE_APP_BASE_API,
            headers: { 'X-Token': getToken() },

            goodsTypeDisabled:false
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            GoodsTypeApi.getAllList().then((response) => {
                this.goodsTypeAllList = response
            }).catch(() => {
            })

            this.dialogVisible = true
            this.$nextTick(() => {
                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = 0
                    this.formData.goodsTypeId = data.goodsTypeId
                    this.formData.parentId = data.parentId
                    this.formData.goodsClassName = ''
                    this.formData.image = ''
                }

                this.goodsTypeDisabled = this.formData.parentId > 0
            })
        },
        handleSuccess(e) {
            this.formData.image = e.data
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        GoodsClassApi.update(that.formData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh',that.formData.parentId)
                        })
                    } else {
                        GoodsClassApi.insert(that.formData).then(() => {
                            that.dialogVisible = false
                            that.$emit('refresh',0)
                        })
                    }
                }
            })
        }
    }
}
</script>

<style lang="scss">
</style>
